package com.awspaas.user.apps.publicservice.util;


import com.actionsoft.bpms.cc.cache.CCCache;
import com.actionsoft.bpms.cc.model.CCModel;
import com.actionsoft.bpms.util.DBSql;
import com.actionsoft.sdk.local.SDK;
import com.actionsoft.sdk.local.api.cc.RDSAPI;
import com.alipay.remoting.util.StringUtils;

public class CmdAtUtil {

    //开始标识
    public static final String openFlag="[$$$===START";
    //结束标识
    public static final String endFlag="END===$$$]";

    public String AtQueryUtil(String viewName,String result, String param,String ccId){

        //拼接sql
        String querySql = "";
        if (null!=param && !param.equals("")) {
            querySql = "SELECT " + result + " FROM " + viewName + " WHERE TO_DO_PROCESSINSTID = '" + param + "'";
        } else {
            querySql = "SELECT " + result + " FROM " + viewName;
        }
        SDK.getLogAPI().consoleInfo("查询语句："+querySql);
        //判断是否外部连接，执行查询
        String queryResult = "";
        System.out.println(ccId);
        try {
            if (null!=ccId && !ccId.equals("")) {
                RDSAPI rdsapi = SDK.getCCAPI().getRDSAPI(ccId);
                queryResult = rdsapi.getString(querySql);
                if(StringUtils.isBlank(queryResult)) {
                    CCModel ccModel = CCCache.getInstance().get(ccId);
                    String ccName = ccModel.getName();
//                    throw new AWSAPIException(ccName+"未查询到结果，请联系三方系统管理员！");
                    return openFlag+"ccName["+ccName+"]未查询到结果，请联系三方系统管理员！"+endFlag;
                }
            } else {
                queryResult = DBSql.getString(querySql);
            }
        } catch (Exception e) {
            e.printStackTrace();
//            SDK.getLogAPI().consoleErr("执行查询异常");
        }

        if (StringUtils.isBlank(queryResult)){
            queryResult=openFlag+"查询数据库表【"+viewName+"】的任务ID为空"+endFlag;
        }

//        if("VIEW_FTS_TRANSACTION".equals(viewName)&&StringUtils.isBlank(queryResult)){
//            throw new ApiException("error","交易视图查询任务ID为空！");
//        }

        return queryResult;
    }

}
